package lzyc.pointsManagement.business.approval.service.impl;

import lzyc.pointsManagement.business.activity.domain.ActivityApply;
import lzyc.pointsManagement.business.activity.domain.ActivityInfo;
import lzyc.pointsManagement.business.activity.mapper.ActivityApplyMapper;
import lzyc.pointsManagement.business.activity.mapper.ActivityInfoMapper;
import lzyc.pointsManagement.business.approval.domain.ApprovalInfo;
import lzyc.pointsManagement.business.approval.mapper.ApprovalInfoMapper;
import lzyc.pointsManagement.business.approval.service.ApprovalInfoService;
import lzyc.pointsManagement.business.config.ApprovalStatus;
import lzyc.pointsManagement.business.config.BizType;
import lzyc.pointsManagement.business.honor.domain.HonorInfo;
import lzyc.pointsManagement.business.points.domain.PointsInfo;
import lzyc.pointsManagement.business.points.service.PointsInfoService;
import lzyc.pointsManagement.business.service.ActivityApplyService;
import lzyc.pointsManagement.common.response.ResponseMessage;
import lzyc.pointsManagement.common.response.ResponseMessageUtility;
import lzyc.pointsManagement.common.utils.StringUtils;
import lzyc.pointsManagement.common.utils.uuid.IdUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

/**
 * (ApprovalInfo)表服务实现类
 *
 * @author yangyang
 * @since 2024-03-10 17:08:49
 */
@Service("approvalInfoService")
public class ApprovalInfoServiceImpl implements ApprovalInfoService {
    @Resource
    private ApprovalInfoMapper approvalInfoMapper;

    @Resource
    private ApprovalInfoService approvalInfoService;

    @Resource
    private ActivityInfoMapper activityInfoMapper;

    @Resource
    private PointsInfoService pointsInfoService;

    @Resource
    private ActivityApplyMapper activityApplyMapper;

    /**
     * 通过ID查询单条数据
     *
     * @param approvalid 主键
     * @return 实例对象
     */
    @Override
    public ApprovalInfo queryById(String approvalId) {
        return this.approvalInfoMapper.queryById(approvalId);
    }

    @Override
    public ApprovalInfo queryByProjectId(String projectId) {
        return approvalInfoMapper.queryByProjectId(projectId);
    }

    /**
     * 查询多条数据
     *
     * @param offset 查询起始位置
     * @param limit 查询条数
     * @return 对象列表
     */
    @Override
    public List<ApprovalInfo> queryAllByLimit(int offset, int limit) {
        return this.approvalInfoMapper.queryAllByLimit(offset, limit);
    }

    /**
     * 新增数据
     *
     * @param approvalInfo 实例对象
     * @return 实例对象
     */
    @Override
    public ApprovalInfo insert(ApprovalInfo approvalInfo) {
        if (StringUtils.isEmpty(approvalInfo.getApprovalId())) {
            approvalInfo.setApprovalId(IdUtils.randomUUID());
        }
        this.approvalInfoMapper.insert(approvalInfo);
        return approvalInfo;
    }

    /**
     * 修改数据
     *
     * @param approvalInfo 实例对象
     * @return 实例对象
     */
    @Override
    public ApprovalInfo update(ApprovalInfo approvalInfo) {
        this.approvalInfoMapper.update(approvalInfo);
        return this.queryById(approvalInfo.getApprovalId());
    }

    /**
     * 通过主键删除数据
     *
     * @param approvalid 主键
     * @return 是否成功
     */
    @Override
    public boolean deleteById(String approvalId) {
        return this.approvalInfoMapper.deleteById(approvalId) > 0;
    }

    @Override
    public ResponseMessage applyReview(String approvalId, String status) {
        ResponseMessage result;
        ApprovalInfo approvalInfo = approvalInfoService.queryById(approvalId);

        if (approvalInfo == null) {
            result = ResponseMessageUtility.createError("当前审批不存在。");
        } else {
            if (ApprovalStatus.APPROVED.getCode().equals(status)) {
                // 审批通过，获取积分
                String activityApplyId = approvalInfo.getProjectId();
                if (!StringUtils.isEmpty(activityApplyId)) {
                    ActivityApply activityApply = activityApplyMapper.getActivityApplyById(activityApplyId);
                    if (activityApply != null) {
                        ActivityInfo activityInfo = activityInfoMapper.selectActivityInfoById(activityApply.getActivityId());
                        if (activityInfo != null) {
                            PointsInfo pointsInfo = pointsInfoService.queryByProjectIdAndUserId(activityInfo.getId(), approvalInfo.getApplyUserId());
                            pointsInfo.setScore(activityInfo.getScore());
                            pointsInfoService.update(pointsInfo);
                            approvalInfo.setStatus(status);
                            approvalInfoService.update(approvalInfo);
                            result = ResponseMessageUtility.createSuccess();
                        } else {
                            result = ResponseMessageUtility.createError("当前活动不存在。");
                        }
                    } else {
                        result = ResponseMessageUtility.createError("当前申请不存在。");
                    }
                } else {
                    result = ResponseMessageUtility.createError("当前申请不存在。");
                }
            } else {
                approvalInfo.setStatus(status);
                approvalInfoService.update(approvalInfo);
                result = ResponseMessageUtility.createSuccess();
            }
        }
        return result;
    }

    @Override
    public int honorApplyReview(String approvalId, String status) {
        int result;
        ApprovalInfo approvalInfo = approvalInfoService.queryById(approvalId);
        if (approvalInfo == null) {
            result = 0;
        } else {
            approvalInfo.setStatus(status);
            approvalInfoService.update(approvalInfo);
            result = 1;
        }
        return result;
    }

    /**
     * 通过projectId审批
     *
     * @param projectId
     * @param status
     * @return
     */
    @Override
    public int reviewByProjectId(String projectId, String status) {
        return approvalInfoMapper.reviewByProjectId(projectId, status);
    }

    /**
     * 通过projectId删除数据
     *
     * @param projectId projectId
     * @return
     */
    @Override
    public int deleteByProjectId(String[] projectId) {
        return approvalInfoMapper.deleteByProjectId(projectId);
    }
}